package com.transaction.manage.localMapper;

import com.transaction.manage.model.dto.FlowInfo;
import com.transaction.manage.model.dto.FlowLog;
import com.transaction.manage.model.vo.HandleVo;
import com.transaction.manage.myMapper.MyMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface FlowLogMapper extends MyMapper<FlowLog> {


    List<FlowLog> selectDayEndHandleList(FlowLog flowLog);

    @Select("SELECT count(*) FROM newest_flow_log l WHERE l.flow_code = #{flowCode} " +
            "AND l.channel_code = #{channelCode} " +
            "AND l.handle_date = #{handleDate} " +
            "AND (l.status = 0 " +
            "   OR (l.status = 1 " +
            "       AND l.step_code != ( select max(step_code) from flow_info i where i.flow_code = #{flowCode}) ))")
    int checkFlowIsProcess(@Param("flowCode") String flowCode,
                           @Param("channelCode") String channelCode,
                           @Param("handleDate") String handleDate);

    void insertFlowLog(@Param("logCode") String logCode,
                       @Param("channelCode") String channelCode,
                       @Param("handleDate") String handleDate,
                       @Param("flowInfo") FlowInfo flowInfo);

    void updateFlowLog(@Param("logCode") String logCode, @Param("handleVo") HandleVo handleVo);

    @Select("SELECT count(*) FROM newest_flow_log l WHERE l.flow_code = #{flowCode} AND l.channel_code = #{channelCode} " +
            "AND l.handle_date = #{handleDate} AND l.step_code = #{stepCode} AND l.`status` = 2")
    int checkStepIsNewestErrorStep(@Param("flowCode") String flowCode,
                                   @Param("channelCode") String channelCode,
                                   @Param("handleDate") String handleDate,
                                   @Param("stepCode") Integer stepCode);

    @Select("select app_sheet_serial_no from ${tableName} where channel_code = #{channelCode} " +
            "and handle_date = #{handleDate} " +
            "group by app_sheet_serial_no having count(*) > 1 ")
    List<String> checkSameAppSheetSerialNo(@Param("tableName") String tableName,
                                           @Param("channelCode") String channelCode,
                                           @Param("handleDate") String handleDate);
}